/**
 * create by young (young2f@outlook.com) on 2020/12/3
 * 组织机构是以树为基础的，自身没有数据
 */
'use strict';
const base = require('../base/serviceBase');
const _ = require('lodash');
const _stringUtils = require('../utils/stringUtils');
const moment = require('moment');
module.exports = class extends base {
    async bindDevice(body) {
        const {ctx} = this;
        const {tenantCode, organizationCode, organizationName, devices} = body;
        _.map(devices, dev => {
            const {deviceCode} = dev;
            const result = ctx.app.dbs.updateOne('Device', {tenantCode, deviceCode}, {
                organizationCode,
                organizationName
            });
        });
        return {status: 'ok', result: 'ok'};
        // const device = await ctx.app.dbs.findOne('Device', {tenantCode, organizationCode});
        //
        // const org = organization ? organization : body;
        //
        // if (organization) {
        //     // org.devices = [...org.devices,...devices];
        //     org.devices = org.devices.concat(devices);
        // }
        //
        // const result = ctx.app.dbs.findOneAndUpdate('Organization', {organizationCode, tenantCode}, org);
        // return {status: 'ok', result};
    }

    async bindUser(body) {
        const {ctx} = this;
        const {tenantCode, organizationCode, userCode} = body;
        const organization = await ctx.app.dbs.findOne('UserOrg', {tenantCode, organizationCode, userCode});
        if (organization) {
            throw new Error('该用户已绑定！');
        }
        const result = ctx.app.dbs.insert('UserOrg', body);
        return {status: 'ok', result};
    }

    async bindUsers(body) {
        const {ctx} = this;
        const {tenantCode, organizationCode, users} = body;
        console.log('bindUserss', body);
        const organization = await ctx.app.dbs.findOne('Tree', {
            tenantCode,
            treeId: organizationCode,
            table: 'organization'
        });
        if (!organization) {
            throw new Error('组织机构不存在！');
        }
        const data = _.clone(body);
        delete data['users'];
        const array = [];
        _.map(users, user => {
            _.mergeWith(user, data);
            array.push(user);
        });
        const result = ctx.app.dbs.insertMany('UserOrg', array);
        return {status: 'ok', result};
    }

    async unbindUser(body) {
        const {ctx} = this;
        const {tenantCode, organizationCode, userCode} = body;
        const organization = await ctx.app.dbs.findOne('Tree', {
            tenantCode,
            treeId: organizationCode,
            table: 'organization'
        });
        if (!organization) {
            throw new Error('组织机构不存在！');
        }
        const result = ctx.app.dbs.deleteOne('UserOrg', {tenantCode, organizationCode, userCode});
        return {status: 'ok', result};
    }

    async unbindDevice(body) {
        const {ctx} = this;
        const {tenantCode, deviceCode} = body;

        const result = ctx.app.dbs.updateOne('Device', {tenantCode, deviceCode}, {organizationCode: undefined});
        return {status: 'ok', result};
        //
        // const organization = await ctx.app.dbs.findOne('Organization', {tenantCode, organizationCode});
        // if (!organization) {
        //     throw new Error('组织机构不存在！');
        // }
        // const {devices} = organization;
        //
        // _.remove(devices, item => {
        //     return item.deviceCode === deviceCode;
        // });
        //
        // const result = await ctx.app.dbs.findOneAndUpdate('Organization', {organizationCode, tenantCode}, {devices});
        // return {status: 'ok', result};
    }
    async bindMeter(body) {
        const {ctx} = this;
        const {tenantCode, organizationCode, organizationName, devices} = body;
        _.map(devices, dev => {
            const {meterCode} = dev;
            const result = ctx.app.dbs.updateOne('Meter', {tenantCode, meterCode}, {
                organizationCode,
                organizationName
            });
        });
        return {status: 'ok', result: 'ok'};

    }
    async unbindMeter(body) {
        const {ctx} = this;
        const {tenantCode, meterCode} = body;
        const result = ctx.app.dbs.updateOne('Meter', {tenantCode, meterCode}, {organizationCode: undefined});
        return {status: 'ok', result};

    }

};


